var express = require('express');
var router = express.Router();
const studentService = require('../service/studentService');

/**
 * @swagger
 * /students:
 *    post:
 *      tags:
 *      - students
 *      summary: 增加学生
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                name:          
 *                  type: string
 *                  description: 学生姓名
 *                age:    
 *                  type: integer
 *                  description: 学生年龄
 *                gender:
 *                  type: string
 *                  description: 学生性别
 *                headImg:
 *                  type: string
 *                  description: 学生头像
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.post('/',async function(req,res){
    let {name,gender,age,headImg} = req.body;
     let data = await studentService.insert({name,gender,age,headImg});
     res.send(data);
});
/**
 * @swagger
 * /students:
 *    get:
 *      tags:
 *      - students
 *      summary: 查询学生信息
 *      parameters:
 *      - name: type
 *        in: query
 *        description: 搜索类型
 *        required: false
 *      - name: value
 *        in: query
 *        description: 搜索值
 *        required: false
 *      - name: current
 *        in: query
 *        description: 当前页码
 *        required: false
 *      - name: pageSize
 *        in: query
 *        description: 每页显示数
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get('/',async function(req,res){
    let {type,value,current,pageSize} = req.query;
    let searchParams = {};
    if(type){
        if(type == "age"){
            searchParams[type] = value;
        }else{
            searchParams[type] = {$regex:value};
        }
    }
    let data = await studentService.find({current:current || 1,pageSize:pageSize || 5},searchParams);
    res.send(data);
});
/**
 * @swagger
 * /students/{id}:
 *    get:
 *      summary: 根据id查询学生
 *      tags:
 *       - students
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 学生id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get('/:id',async function(req,res){
    let {id} = req.params;
    let data = await studentService.findById(id);
    res.send(data);
});
/**
 * @swagger
 * /students/{id}:
 *    delete:
 *      summary: 根据id删除学生
 *      tags:
 *       - students
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 学生id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.delete('/:id',async function(req,res){
    let {id} = req.params;
    let data = await studentService.delete(id);
    res.send(data);
});
/**
 * @swagger
 * /students/{id}:
 *    put:
 *      summary: 根据id修改学生
 *      tags:
 *       - students
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 学生id
 *        required: true
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                name:          
 *                  type: string
 *                age:    
 *                  type: integer
 *                gender:
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.put('/:id',async function(req,res){
    let {id} = req.params;
    let {name,gender,age} = req.body;
    console.log({_id:id,name,gender,age})
    let data = await studentService.update({_id:id,name,gender,age});
    res.send(data);
});

module.exports = router;